package com.oitsm.dispatcher;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import com.oitsm.collection.CollectionExcutor;
import com.oitsm.dispatcher.task.CollectTasker;
import com.oitsm.dispatcher.task.SysTasker;
import com.oitsm.dispatcher.task.Tasker;

/**
 * 任务管理器
 * @author Arab Runner
 * Jul 24, 2011 1:25:40 PM
 */
public class TaskService {
	/**
	 * 任务队列
	 */
	private static BlockingQueue<Tasker> taskQueue = new LinkedBlockingQueue<Tasker>();
	
	/**
	 * 任务分发器
	 */
	private static ScheduledExecutorService taskDispatcherExcutor = null;
	
	public static void init()throws Exception{
		taskDispatcherExcutor = Executors.newSingleThreadScheduledExecutor();
		taskDispatcherExcutor.scheduleWithFixedDelay(new Runnable(){
			public void run() {
				while(!taskQueue.isEmpty()){
					Tasker t = taskQueue.poll();
					
					// 采集任务 - 现在为单机模式.
					if(t instanceof CollectTasker){
						CollectionExcutor.addTask((CollectTasker)t);
					
					// 系统任务
					}else if(t instanceof SysTasker){
						SysTaskExcutor.addTask((SysTasker)t);
					}
				}
			}
		}, 0, 200, TimeUnit.MILLISECONDS);
	}
	
	public static void addTask(Tasker t){
		taskQueue.add(t);
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {

	}
	
}
